var util = require("../../../libs/util.js");
var amapFile = require('../../../libs/amap-wx.js');
var amap, mapCtx;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    latitude: 0,//纬度 
    longitude: 0,//经度 
    speed: 0,//速度 
    accuracy: 16,//位置精准度 
    navLat: 0,//导航纬度 
    navLon: 0,//导航经度 
    markers: [],
    polyline: [],
    "include-points": [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    that.setData({
      id:e.id
    })
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    amap = new amapFile.AMapWX({ key: 'a404e7cb1dcd34bb623e46fc3e46691e' });
    //手机定位
    that.getLocation();
    that.loadData();
  },
  loadData: function () {
    var that = this;
    util.getData("/atapi/vehicle/vehicle/gps_monitor", {
      id: that.data.id
    }, function (res) {
      console.log(res)
      var data = res.data.data;
      
      if (res.data.success) {
        var points = [];
        for (var j = 0; j < data.length; j++) {
          //console.log(data[j].gps)
          points.push({
            longitude: parseFloat(data[j].gps.split(',')[1]),
            latitude: parseFloat(data[j].gps.split(',')[0])
          })
        }
        //console.log()
        that.setData({
          polyline: [{
            points: points,
            color: "#0091ff",
            width: 3
          }],
          stopPoint: res.data.stopPoint
        });
        // if (data.paths[0] && data.paths[0].distance) {
        //   var duration = Number(data.paths[0].duration) + pathData.length * 60;
        //   var hourToTime = util.hourToTime(duration);
        //   that.setData({
        //     distance: (data.paths[0].distance / 1000).toFixed(1),
        //     duration: Number(duration).toFixed(2),
        //     duration_hour: hourToTime.hour,
        //     duration_minute: hourToTime.minute,
        //     'delivery.distance': (data.paths[0].distance / 1000).toFixed(1),
        //     'delivery.duration': Number(duration).toFixed(2)
        //   });
        // }
      }

      var pointData = res.data.pointData;
      that.setData({
        pointData: pointData
      })
      if (pointData.length > 0) {
        that.setMarkers()
        mapCtx.includePoints({
          padding: [10],
          points: that.data.lalt
        })
      }

      if (that.data.polyline.length == 0){
        wx.showModal({
          title: '温馨提示',
          content: '没有行车轨迹数据',
          success(res) {
            // wx.navigateBack({
            //   delta: 1
            // })
          }
        })
      }
      wx.hideLoading();
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    mapCtx = wx.createMapContext('map');
  },
  getLocation(callback) {
    var that = this;
    //获取手机定位坐标
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res.latitude)
        var latitude = res.latitude;
        var longitude = res.longitude;
        that.setData({
          longitude: longitude,
          latitude: latitude
        });
        if (callback) {
          callback(longitude, latitude)
        }
      }
    });
  },
  setMarkers: function () {
    var that = this;
    var pointData = that.data.pointData;
    var markers = [], lalt = [];
    var icon = '/images/space.png', width = 0, height = 0;
    for (var i = 0; i < pointData.length; i++) {
      var coord = String(pointData[i].wh_coord || pointData[i].s_coord).split(',');
      width = 24, height = 24;
      if (i == 0) {
        icon = '/images/qidian.png';
      }else if (i == pointData.length - 1) {
        icon = '/images/zhongdian.png';
      } else{
        icon = '/images/tujing.svg';
      }
      markers.push({
        id: markers.length,
        title: pointData[i].wh_name || pointData[i].s_name,
        callout: {
          content: pointData[i].wh_name || pointData[i].s_name,
          fontSize: 11,
          color: '#ffffff',
          borderRadius: 2,
          bgColor: '#d81e06',
          padding: 3,
          display: 'ALWAYS'
        },
        iconPath: icon,
        width: width,
        height: height,
        latitude: coord[0],
        longitude: coord[1]
      });
      lalt.push({
        latitude: coord[0],
        longitude: coord[1]
      })
    }
    if (that.data.stopPoint && that.data.stopPoint.length > 0){
      icon = '/images/space.png', width = 0, height = 0;
      var stopPoint = that.data.stopPoint;
      for (var i = 0; i < stopPoint.length; i++) {
        var coord = String(stopPoint[i].gps).split(',');
        markers.push({
          id: markers.length,
          title: "停留时间：" + stopPoint[i].duration+"分钟",
          callout: {
            content: "停留时间：" + stopPoint[i].duration + "分钟",
            fontSize: 11,
            color: '#ffffff',
            borderRadius: 2,
            bgColor: '#1974b3',
            padding: 3,
            display: 'ALWAYS'
          },
          iconPath: icon,
          width: width,
          height: height,
          latitude: coord[0],
          longitude: coord[1]
        });
      }
      
    }
    that.setData({
      markers: markers,
      lalt: lalt
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})